package pkg.nudroid.translator;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class LayoutBuilder {
	
	String app_name;
	String XML_output;
	String XML_output_;
	String AndroidManifest_output;
	
	public LayoutBuilder() {
		//createAndroidManifest();
		XML_output_ = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n";
	}
	
	/** Sets App Name **/
	public void setAppName(String app_name) {
		this.app_name = app_name;
	}
	
	/** Creates the Android Manifest with one Activity with Name name **/
	public void createAndroidManifest(String name) {
		
		try {
		// first file
		  BufferedWriter out1 = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/pkg/nudroid/AndroidManifest.xml"));
		  
		  AndroidManifest_output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";		
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"";
		  out1.write(AndroidManifest_output); out1.newLine();	
		  AndroidManifest_output = "\tpackage=\"pkg.nudroid\"";
		  out1.write(AndroidManifest_output); out1.newLine();	
		  AndroidManifest_output = "\tandroid:versionCode=\"1\"";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "\tandroid:versionName=\"1.0\">";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "\t\t\t\t<uses-permission android:name=\"android.permission.INTERNET\" />";
		  out1.write(AndroidManifest_output); out1.newLine();	
		  AndroidManifest_output = "\t<application android:icon=\"@drawable/icon\" android:label=\"" + app_name + "\">";
		  out1.write(AndroidManifest_output); out1.newLine();
		  
		// It should make all page activity
		  AndroidManifest_output = "\t\t<activity android:name=\"." + name + "\"";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "\t\t\tandroid:label=\"" + app_name + "\">";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "\t\t\t\t<intent-filter>";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "\t\t\t\t\t<action android:name=\"android.intent.action.MAIN\" />";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "\t\t\t\t\t<category android:name=\"android.intent.category.LAUNCHER\" />";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "\t\t\t\t</intent-filter>";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "\t\t</activity>	";
		  out1.write(AndroidManifest_output); out1.newLine();
		  
		  
		  AndroidManifest_output = "\t</application>";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "\t<uses-sdk android:minSdkVersion=\"7\" />";
		  out1.write(AndroidManifest_output); out1.newLine();
		  AndroidManifest_output = "</manifest> ";
		  out1.write(AndroidManifest_output); out1.newLine();
		  out1.close();  
		  
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/** Creates the Default Properties file **/
	public void createDefaultProperties() {
	
		try {
		// second file
		
		  BufferedWriter out2 = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/pkg/nudroid/default.properties"));

		  String s2 = "# Indicates whether an apk should be generated for each density.";
		  out2.write(s2); out2.newLine();
		  s2 = "split.density=false";		
		  out2.write(s2); out2.newLine();
		  s2 = "# Project target.";
		  out2.write(s2); out2.newLine();	
		  s2 = "target=android-7";
		  out2.write(s2); out2.newLine();	
		  out2.close();		// second file
		  
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/** Appends a string to the layout **/
	public void appendToLayout(String layout) {
		XML_output += layout;
	}
	
	/** Prints the layout to a file.xml **/
	public void commitLayout()  {
		BufferedWriter out;
		
		try {
			out = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/pkg/nudroid/"+page_id+".xml"));
			System.out.println(XML_output);
			out.write(XML_output);
			out.newLine();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/** Prints any xml output to a file.xml **/
	public void commitLayout(String output, String file) {
		BufferedWriter out;
		
		try {
			out = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/pkg/nudroid/"+file+".xml"));
			out.write(XML_output_);
			out.write(output);
			out.newLine();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
/** THESE ARE VARIOUS METHODS TO RETURN APPROPRIATE XML CODE **/

	/** PAGES **/
	String page_id;
	String page_name;
	
	public String makePage(String page_id, String page_name) {
		this.page_id = page_id;
		this.page_name = page_name;
		
		String temp;
		temp = 
			"<RelativeLayout " +
		    "android:id=\"@+id/"+page_id+"_page\" " +
		    "android:layout_width=\"fill_parent\" " +
		    "android:layout_height=\"fill_parent\" " +
		    "xmlns:android=\"http://schemas.android.com/apk/res/android\" " +
		    ">\n" +
		    "<TextView " +
		        "android:id=\"@+id/"+page_id+"_label\" " +
		        "android:layout_width=\"wrap_content\" " +
		        "android:layout_height=\"wrap_content\" " +
		        "android:text=\"" +page_name+ "\" " +
		        //"android:layout_alignParentTop=\"true\" "+
		        //"android:layout_alignParentLeft=\"true\""+
		        "/>\n";
		return temp;
		
	}
	
	public String returnPage() {
		return "</RelativeLayout>\n";
	}

	/** TABLES **/
	int table_columns;
	int table_row_count;
	
	public String makeTable(int cols) {
		String temp;
		
		// initialize table
		temp = 
		"<TableLayout " +
		    "android:layout_height=\"fill_parent\" " +
		    "android:layout_width=\"fill_parent\" " +
		    "xmlns:android=\"http://schemas.android.com/apk/res/android\" " +
		    "android:background=\"#000044\"> \n";
		
		// first table row
		temp += 
		"<TableRow> \n";
		
		table_columns = cols;
		table_row_count = 0;   
		
		return temp;
	}
	
	public String addToTable(String text, int width) {
		
		String temp = "";
		
		// Separate rows based on col max
		if (table_row_count >= table_columns) {
			temp += 
				"</TableRow> \n" + 
				"<TableRow> \n";
			table_row_count = 0;
		}
		
		temp +=
			"<TextView " +
		    "android:text=\""+ text +"\" " +
		        "android:width=\""+width+"px\" " +
		        "/>\n";
		table_row_count++;
		
		return temp;

	}
	
	public String returnTable() {
		return "</TableRow>\n</TableLayout>\n";
	}
}
